<?php

namespace App\Models;


/**
 * App\Models\Member
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $group_id 分组ID
 * @property string $username 姓名
 * @property string $mobile 电话
 * @property string $height 身高
 * @property string $weight 体重
 * @property int $age 年龄
 * @property int $sex 1男2女
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string $id_card 身份证号
 * @property string|null $introduce 个人介绍
 * @property-read \App\Models\Group $group
 * @property-read \App\Models\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|Member newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Member newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Member query()
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereAge($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereGroupId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereHeight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereIdCard($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereIntroduce($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereSex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereUsername($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Member whereWeight($value)
 * @mixin \Eloquent
 */
class Member extends Base{

    protected $table = 'members';

    const SEX_1 = 1;
    const SEX_2 = 2;
    const SEX_TEXT = [
        self::SEX_1 => '男',
        self::SEX_2 => '女',
    ];

    protected $fillable = [
        'id','group_id','user_id','username','mobile','height','weight','sex','created_at','updated_at','age','id_card','introduce'
    ];

    public function user(){
        return $this->belongsTo('App\Models\User', 'user_id', 'id')
            ->withDefault([
                'username' => ''
            ]);
    }

    public function group(){
        return $this->belongsTo('App\Models\Group', 'group_id', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*']){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('username','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['mobile']) && $params['mobile']) {
                    $query ->where('mobile',$params['mobile']);
                }
                if(isset($params['uid']) && $params['uid']) {
                    $query ->where('user_id',$params['uid']);
                }
            })->with(['user' => function ($query) {
                $query->select(['username','id']);
            }])->with(['group' => function ($query) {
                $query->select(['name','id']);
            }])->orderBy('id', 'desc')->paginate(config('app.limit'));
        $this->stateToText($lists,[
            'sex' => self::SEX_TEXT,
        ]);
        return $lists;
    }


    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function getWList($params = [], $fields = ['*']){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['uid']) && $params['uid']) {
                    $query ->where('user_id',$params['uid']);
                }
                if(isset($params['gid']) && $params['gid']) {
                    $query ->where('group_id',$params['gid']);
                }
            })->orderBy('id', 'asc')
            ->paginate(config('app.wechat_limit'));
        return $lists;
    }

}
